package main

func canCompleteCircuit(gas []int, cost []int) int {
    spare := 0
	minIndex := 0
	minSpare := 1 << 30
	for i := range gas {
		spare += gas[i] - cost[i]
		if spare < minSpare {
			minSpare = spare
			minIndex = i
		}
	}
	if spare < 0 {
		return -1
	}
	return (minIndex + 1) % len(gas)
}